package cn.itsource.org.controller;

import cn.itsource.basic.query.BaseQuery;
import cn.itsource.basic.utils.AjaxResult;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/emp")
@Api(value = "EmployeeController",tags = {"员工接口文档"})
public class EmployeeController {

    //注入service层
    @Autowired
    private IEmployeeService employeeService;


    /**
     * 分页查询
     * @param query
     * @return
     */
    @PostMapping("/list")
    @ApiOperation(value = "getPageList",notes = "返回所有的分页数据")
    public AjaxResult getPageList(@ApiParam(value = "分页参数",name = "分页查询")@RequestBody BaseQuery query){
        return AjaxResult.creatSuccess(employeeService.getAllByPage(query));
    }

    @PostMapping("/saveOrUpdate")
    public AjaxResult saveOrUpdate(@RequestBody Employee employee){
        try {
            employeeService.saveOrUpdate(employee);
            return AjaxResult.creatSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.creatError("操作失败，找土皮回炉");
        }

    }


    @DeleteMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){

        try {
            employeeService.delete(id);
            return AjaxResult.creatSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.creatError("删除操作失败，找土皮回炉");
        }
    }

    //查询所有的员工信息
    @GetMapping("/all")
    public AjaxResult getAllEmp(){

        return AjaxResult.creatSuccess(employeeService.getAll());
    }

    //查询所有部门经理信息
    @GetMapping("/getManages")
    public AjaxResult getManages(){

        return AjaxResult.creatSuccess(employeeService.getManages());
    }


}
